<template>
  <div class="login">
    <div class="login-container">
      <div class="login-title__pic">{{ hisTitle.loginTitle }}</div>
      <div class="login-content">
        <div class="login-content__left"></div>
        <div class="login-content__right">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">

            <div class="welcome-text">欢迎登录</div>
            <el-input class="login-account" type="text" v-model="loginForm.username" autocomplete="off"
                      placeholder="请输入账号">
              <template #prefix>
                <div class="login-account__icon"></div>
              </template>
            </el-input>
            <el-input class="login-password" type="password" v-model="loginForm.password" autocomplete="off"
                      placeholder="请输入密码">
              <template #prefix>
                <div class="login-password__icon"></div>
              </template>
            </el-input>
            <div class="flex-1"></div>
            <div class="login-submit" @click="handleLogin">
                <span v-if="!loading"  >登 录</span>
                <span v-else>登 录 中...</span>
              <i class="el-icon-right"></i>
            </div>
          </el-form>
        </div>
      </div>
<!--      <p class="archival-information">沪ICP备15050430号-1ICP 证沪B2-20180118 沪公网安备 31011502008153号</p>-->
    </div>
  </div>
</template>

<script>
  import { getCodeImg,loginTitle } from "@/api/login";
  import Cookies from "js-cookie";
  import { encrypt, decrypt } from '@/utils/jsencrypt'

  export default {
    name: "Login",
    data() {
      return {
        loading: false,
        hisTitle: {},
        loginRules: {
          username: [
            { required: true, trigger: "blur", message: "请输入您的账号" }
          ],
          password: [
            { required: true, trigger: "blur", message: "请输入您的密码" }
          ]
        },
        loginForm: {
          username: '',
          password: ''
        }
      };
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      // this.getCode();
      // this.getCookie();
      this.getTitle();
    },
    methods: {
      getTitle() {
        loginTitle().then(res => {
          this.hisTitle=res.data
        });
      },
      getCode() {
        getCodeImg().then(res => {
          this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
          if (this.captchaOnOff) {
            this.codeUrl = "data:image/gif;base64," + res.img;
            this.loginForm.uuid = res.uuid;
          }
        });
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
        };
      },
      handleLogin() {
          localStorage.setItem("menuId", "1"); // 清空menuId
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            this.$store.dispatch("Login", this.loginForm).then(() => {
              // this.$router.push({ path: this.redirect || "/home" }).catch(()=>{});
			  this.$router.push({path: "/home"}).catch(() => {
			            })
            }).catch(() => {
              this.loading = false;
            });
          }
        });
      }
    }
  };
</script>



<style rel="stylesheet/scss" lang="scss" scoped>
.login-form{
  display: flex;
  flex-direction: column;
  height: 100%;
}

.flex-1 {
  flex: 1;
}

  .login {
    width: 100%;
    min-width: 1200px;
    height: 100%;
    background-image: url("~@/assets/images/login/login-background.png");
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;

    .login-container {
      position: relative;
      width: 1189px;
      height: 639px;
      background-color: #fff;

      .login-title__pic {
        position: absolute;
        top: 0;
        left: 0;
        width: 494px;
        height: 64px;
        background-image: url("~@/assets/images/login/login-title.png");
        background-repeat: no-repeat;
        color: #FCFDFF;
        font-weight: bold;
        font-size: 25px;
        line-height: 60px;
        padding-left: 22px;

      }

      .login-content {
        display: flex;
        margin-top: 64px;
        padding: 57px 92px 116px 72px;
        width: 100%;
        height: calc(100% - 64px);
        background-image: url("~@/assets/images/login/login-content-bg.png");
        background-position: left bottom;
        background-repeat: no-repeat;

        &__left {
          // background-color: pink;
          width: 551px;
          height: 391px;
          flex: 1;
          background-image: url("~@/assets/images/login/login-left.png");
        }

        &__right {
          width: 474px;
          height: 366px;
          padding-left: 49px;
          display: flex;
          flex-direction: column;

          .welcome-text {
            color: #3B3B3B;
            font-weight: bold;
            font-size: 26px;
            line-height: 48px;
            user-select: none;
          }

          .login-account {
            margin-top: 55px;

            &__icon {
              background-image: url("~@/assets/images/login/account.png");
            }
          }

          .login-password {
            margin-top: 30px;

            &__icon {
              background-image: url("~@/assets/images/login/lock.png");
              background-repeat: no-repeat;
            }
          }

          .login-account,
          .login-password {
            ::v-deep .el-input__inner {
              display: block;
              height: 59px;
              font-size: 18px;
              padding-left: 51px;
            }

            ::v-deep .el-input__prefix {
              display: flex;
              align-items: center;
              justify-content: flex-start;
              width: 51px;
              height: 100%;
              padding-left: 16px;

              .login-account__icon,
              .login-password__icon {
                width: 24px;
                height: 22px;
              }
            }
          }

          .login-submit {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 59px;
            background-color: #4373F1;
            color: #fff;
            line-height: 59px;
            border-radius: 5px;
            padding-left: 27px;
            padding-right: 21px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            user-select: none;

            .el-icon-right {
              font-size: 22px;
            }
          }
        }
      }

      .archival-information {
        position: absolute;
        bottom: -53px;
        left: 50%;
        transform: translateX(-50%);
        color: #6B6B6B;
        margin: 0;
        font-size: 14px;
        font-weight: 500;
      }
    }
  }

  .flex-1 {
    flex: 1;
  }
</style>
